package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类管理相关接口
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategorController {
    @Resource
    private CategorService categorService;

    /**
     * 分类管理分页查询
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分类管理分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){

        PageResult pageResult = categorService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用分类
     * @return
     */
    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result statusUpdate(@PathVariable Integer status,Long id){
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);

        categorService.statusUpdate(category);
        return Result.success();
    }

    /**
     * 新增分类
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result add(@RequestBody Category category){
        categorService.add(category);

        return Result.success();
    }

    /**
     * 根据ID删除分类
     * @return
     */
    @ApiOperation("根据ID删除分类")
    @DeleteMapping
    public Result delete(Long id){

        categorService.delete(id);
        return Result.success();
    }

    /**
     * 修改分类
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categorService.update(category);
        return Result.success();
    }

    /**
     * 更具类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result listPage(Integer type){
        List<Category> category = categorService.listPage(type);
        return Result.success(category);
    }
}
